cmake_minimum_required(VERSION 3.12)

# set(CMAKE_CXX_COMPILER clang++)
if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

project(mallocvis LANGUAGES CXX)

add_compile_options($<$<CXX_COMPILER_ID:GNU,Clang>:-Wall$<SEMICOLON>-Wextra$<SEMICOLON>-Werror=return-type$<SEMICOLON>>)
add_link_options($<$<CXX_COMPILER_ID:GNU>:-rdynamic>)
# add_compile_options($<$<CXX_COMPILER_ID:Clang>:-stdlib=libc++>)
# add_link_options($<$<CXX_COMPILER_ID:Clang>:-stdlib=libc++>)

add_library(mallocvis SHARED malloc_hook.cpp plot_actions.cpp)
find_package(Threads)
if (Threads_FOUND)
    target_link_libraries(mallocvis PRIVATE Threads::Threads)
    target_compile_definitions(mallocvis PRIVATE -DHAS_THREADS)
endif()

add_executable(example example.cpp)
target_link_libraries(example PRIVATE mallocvis)

find_package(tsl-robin-map)
if (tsl-robin-map_FOUND)
    target_link_libraries(example PRIVATE tsl::robin_map)
    target_compile_definitions(example PRIVATE -DHAS_TSL)
endif()

find_package(absl)
if (absl_FOUND)
    target_link_libraries(example PRIVATE absl::flat_hash_set absl::node_hash_set)
    target_compile_definitions(example PRIVATE -DHAS_ABSL)
endif()

find_package(OpenVDB)
if (OpenVDB_FOUND)
    add_executable(vdb_example vdb_example.cpp)
    target_link_libraries(vdb_example PRIVATE mallocvis)
    target_link_libraries(vdb_example PRIVATE OpenVDB::openvdb)
endif()

if (Threads_FOUND)
    add_executable(mt_example mt_example.cpp)
    target_link_libraries(mt_example PRIVATE mallocvis)
    target_link_libraries(mt_example PRIVATE Threads::Threads)
endif()

find_package(OpenGL)
find_package(glfw3)
find_package(glm)
if (OpenGL_FOUND AND glfw3_FOUND AND glm_FOUND)
    add_executable(visualizer visualize.cpp)
    target_link_libraries(visualizer PRIVATE OpenGL::GL)
    target_link_libraries(visualizer PRIVATE glfw)
    target_link_libraries(visualizer PRIVATE glm)
endif()
